package com.gxuwz.dao.impl;

import com.gxuwz.dao.ClassesDao;
import com.gxuwz.entity.Classes;
import com.gxuwz.entity.Department;
import com.gxuwz.entity.Student;
import com.gxuwz.entity.vo.ClassesVo;
import com.gxuwz.entity.vo.PageVo;
import com.gxuwz.result.PageInfo;
import com.gxuwz.util.PageUtil;
import com.sun.org.apache.xalan.internal.xsltc.compiler.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class ClassesDaoImpl implements ClassesDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public PageVo selectClassesAll(Classes entity, PageInfo pageInfo,String instID) {
        String sql = "select classID,className,major,grade,depName from sys_classes,sys_department " +
                "where sys_classes.del_flag = 0 and sys_classes.depID = sys_department.depID and " +
                "sys_classes.instID = ? and sys_department.del_flag = 0 ";
        BeanPropertyRowMapper<ClassesVo> classesBeanPropertyRowMapper = new BeanPropertyRowMapper<ClassesVo>(ClassesVo.class);
        List<Object> example = new ArrayList<Object>();
        PageUtil pageUtil = new PageUtil();
        if(entity==null){
            Object[] params = new Object[]{instID};
            PageVo pageVo = pageUtil.queryForPage(sql, pageInfo.getPageNum(), pageInfo.getPageSize(), params, classesBeanPropertyRowMapper,jdbcTemplate);
            return pageVo;
        } else {
            Object[] params;
            example.add(instID);
            if(entity.getClassID() != null && !entity.getClassID().equals("")){
                sql += "and classID like ?";
                example.add("%"+entity.getClassID()+"%");
            }
            if(entity.getClassName() != null && !entity.getClassName().equals("")){
                sql += "and className like ?";
                example.add("%"+entity.getClassName()+"%");
            }
            if(entity.getDepID() != null && !entity.getDepID().equals("")){
                sql += "and depID like ?";
                example.add("%"+entity.getDepID()+"%");
            }
            if(entity.getMajor() != null && !entity.getMajor().equals("")) {
                sql += "and major like ?";
                example.add("%"+entity.getMajor()+"%");
            }
            params = example.toArray();
            PageVo pageVo = pageUtil.queryForPage(sql, pageInfo.getPageNum(), pageInfo.getPageSize(), params, classesBeanPropertyRowMapper,jdbcTemplate);
            return pageVo;
        }
    }


    public Classes selectClassByID(String classId) {
        String sql = "select * from sys_classes where del_flag = 0 and classID = ? ";
        BeanPropertyRowMapper<Classes> classesBeanPropertyRowMapper = new BeanPropertyRowMapper<Classes>(Classes.class);
        Object[] params = new Object[]{classId};

        try{
            Classes classes = jdbcTemplate.queryForObject(sql, classesBeanPropertyRowMapper, params);
            return classes;
        }catch(Exception e){
            return null;
        }
    }


    public int insertClass(Classes entity) {
        String sql = "insert into sys_classes values(?,?,?,?,?,?,0)";
        Object[] params = new Object[]{entity.getClassID(),entity.getClassName(),entity.getDepID(),entity.getMajor(),entity.getGrade(),entity.getInstID()};
        int insert = jdbcTemplate.update(sql,params);
        return insert;
    }


    public int updateClass(Classes entity, String oldId) {
        String sql = "update sys_classes set classID = ? , className = ? , depID = ? ,major = ? , grade = ? " +
                "where classID = ?";
        Object[] params = new Object[]{entity.getClassID(),entity.getClassName(),entity.getDepID(),entity.getMajor(),entity.getGrade(),oldId};
        int update = jdbcTemplate.update(sql,params);
        return update;
    }


    public int deleteClassById(String classId) {
        String sql = "update sys_classes set del_flag = 1 where classID = ?";
        Object[] params = new Object[]{classId};
        int delete = jdbcTemplate.update(sql,params);
        return delete;
    }


    public List<String> selectClassesIds() {
        String sql = "select classID from sys_classes";
        List<String> ids = jdbcTemplate.queryForList(sql, String.class);
        return ids;
    }

    public List<Classes> selectClassesIdsAndName(String instID) {
        String sql = "select classID,className from sys_classes where del_flag = 0 and instID = ?";
        BeanPropertyRowMapper<Classes> classesBeanPropertyRowMapper = new BeanPropertyRowMapper<Classes>(Classes.class);
        Object[] params = new Object[]{instID};
        List<Classes> classesList = jdbcTemplate.query(sql, classesBeanPropertyRowMapper,params);
        return classesList;
    }

    public List<String> selectClassGrades(String instID) {
        String sql = "select grade from sys_classes where del_flag = 0 and instID = ? ";
        Object[] params = new Object[]{instID};
        List<String> grades = jdbcTemplate.queryForList(sql, String.class,params);
        return grades;
    }

    public List<String> selectMajors(String instID) {
        String sql = "select major from sys_classes where del_flag = 0 and instID = ?";
        Object[] params = new Object[]{instID};
        List<String> majors = jdbcTemplate.queryForList(sql, String.class,instID);
        return majors;
    }


    @Override
    public List<Classes> selectClassesByDepID(String depId) {
        String sql = "select * from sys_classes where depID = ? and del_flag = 0";
        BeanPropertyRowMapper<Classes> classesBeanPropertyRowMapper = new BeanPropertyRowMapper<>(Classes.class);
        Object[] params = new Object[]{depId};
        List<Classes> query = jdbcTemplate.query(sql, classesBeanPropertyRowMapper, params);
        return query;
    }

    @Override
    public List<Classes> selectClassesByInstID(String instId) {
        String sql = "select * from sys_classes where instID = ? and del_flag = 0";
        BeanPropertyRowMapper<Classes> classesBeanPropertyRowMapper = new BeanPropertyRowMapper<>(Classes.class);
        Object[] params = new Object[]{instId};
        List<Classes> query = jdbcTemplate.query(sql, classesBeanPropertyRowMapper, params);
        return query;
    }

    @Override
    public List<String> selectClassesIdsByNoDel() {
        String sql = "select classID from sys_classes where del_flag = 0";
        List<String> strings = jdbcTemplate.queryForList(sql, String.class);
        return strings;
    }


}
